/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "propertyform.h"
#include "ui_propertyform.h"

PropertyForm::PropertyForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PropertyForm)
{
    ui->setupUi(this);
}

PropertyForm::~PropertyForm()
{
    delete ui;
}

void PropertyForm::setPropertyValue(const QString& key, const QString &value) {
    int index = findRowWithKey(key);
    if(index >= 0 ) {
        QTableWidgetItem *item = ui->propertiesTable->item(index, 1);
        if(item)
            item->setText(value);

        // Save the changes

        if(!value.isEmpty())
            properties.insert(key, value);
        else
            properties.remove(key);
    }
}

void PropertyForm::savePropertyValues() {
    for(int i = 0; i<ui->propertiesTable->rowCount(); ++i) {
        const QString& key = ui->propertiesTable->item(i,0)->data(Qt::UserRole).toString();
        const QString& value = ui->propertiesTable->item(i,1)->text();
        if(!value.isEmpty())
            properties.insert(key, value);
        else
            properties.remove(key);
    }
}

QHash<QString, QString> PropertyForm::getProperties() const {
    return properties;
}

void PropertyForm::addProperty(const GameProperty &property, const QString &key) {
    QTableWidgetItem *propertyCell = new QTableWidgetItem(property.getName());
    propertyCell->setData(Qt::UserRole, QVariant(key));
    propertyCell->setFlags(propertyCell->flags() & ~Qt::ItemIsEditable);
    int row = ui->propertiesTable->rowCount();
    ui->propertiesTable->insertRow(row);
    ui->propertiesTable->setItem(row, 0, propertyCell);
    ui->propertiesTable->setItem(row, 1, new QTableWidgetItem());

    keyIndex.insert(key, row);
}

void PropertyForm::updateProperty(const QString &oldKey, const GameProperty &newProperty,
                                  const QString &newKey) {
    int row = findRowWithKey(oldKey);
    if(row > -1) {
        QTableWidgetItem *item = ui->propertiesTable->item(row, 0);
        item->setText(newProperty.getName());
        item->setData(Qt::UserRole, QVariant(newKey));
    }
}

void PropertyForm::removeProperty(const QString& key) {
    int row = findRowWithKey(key);

    if(row > -1) {
        ui->propertiesTable->removeRow(row);
        rebuildIndex();
    }
}

inline int PropertyForm::findRowWithKey(const QString &key) {
    return keyIndex.value(key, -1);
}

void PropertyForm::rebuildIndex() {
    keyIndex.clear();
    for(int i = 0; i<ui->propertiesTable->rowCount(); ++i)
        keyIndex.insert(ui->propertiesTable->item(i,0)->data(Qt::UserRole).toString(), i);
}

void PropertyForm::clearAllValues() {
    for(int i = 0; i<ui->propertiesTable->rowCount(); ++i)
        ui->propertiesTable->item(i, 1)->setText("");
}

void PropertyForm::clearAll() {
    keyIndex.clear();
    ui->propertiesTable->setRowCount(0);
}

void PropertyForm::accept()
{
    savePropertyValues();
    QDialog::accept();
}

void PropertyForm::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
    QWidget::changeEvent(event);
}
